package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.entity.BindInsulinEntity;
import com.ywjh.inter.entity.DocProfileEntity;
import com.ywjh.inter.entity.InstitutionEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.entity.UsersHXEntity;
import com.ywjh.inter.service.InstService;
import com.ywjh.inter.service.InsulinService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Search extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2262925259059192383L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();

		switch (act) {
		case "users_search":
			String keyword = data.getString("key_word");
			List<UsersEntity> list = UsersService.getInstance().searchByKeyword(keyword);
			JSONArray array = new JSONArray();
			for (UsersEntity users : list) {
				UsersHXEntity hx = UsersService.getInstance().findHXUserByUserId(users.getId());
				JSONObject userjson = new JSONObject();
				userjson.put("id", users.getId());
				userjson.put("loginname", users.getLoginname());
				userjson.put("is_doc", users.getIs_doc());
				userjson.put("register_time", StrUtil.getYYYYMMDD(users.getRegister_time().getTime()));
				userjson.put("last_login_time", StrUtil.getYYYYMMDD(users.getLast_login_time().getTime()));
				userjson.put("hx_username", hx.getUsername());
				userjson.put("hx_password", hx.getPassword());
				userjson.put("head_url", SystemConfig.SERVER_DOMAIN+users.getHead_url());
				if (users.getIs_doc() == 0) {
					ProfileEntity entity = UsersService.getInstance().getProfileByUserId(users.getId());
					if (entity != null) {
						BindInsulinEntity bindInsulinEntity= InsulinService.getInstance().findInsulinByPatientId(users.getId());
						userjson.put("profile", entity.toJson(users, bindInsulinEntity));
					}
				} else if (users.getIs_doc() == 1) {
					DocProfileEntity docProfileEntity = UsersService.getInstance().findDocProfileByUserId(users.getId());
					if (docProfileEntity != null) {
						JSONObject profile = new JSONObject();
						profile.put("user_id", docProfileEntity.getUser_id());
						profile.put("name", docProfileEntity.getName());
						profile.put("title", docProfileEntity.getTitle());
						profile.put("inst_id", docProfileEntity.getInst_id());
						profile.put("good", docProfileEntity.getGood());
						profile.put("address",docProfileEntity.getAddress());
						profile.put("idcard",docProfileEntity.getIdcard());
						profile.put("nickname",docProfileEntity.getNickname());
						InstitutionEntity keshi = InstService.getInstance().findById(docProfileEntity.getInst_id());
						if (keshi != null) {
							profile.put("keshi", keshi.getName());
							InstitutionEntity bumen = InstService.getInstance().findById(keshi.getPid());
							if (bumen != null) {
								profile.put("bumen", bumen.getName());
								InstitutionEntity yiyuan = InstService.getInstance().findById(bumen.getPid());
								profile.put("yiyuan", yiyuan.getName());
							}
						}
						profile.put("memo", docProfileEntity.getMemo());
						profile.put("gender",docProfileEntity.getGender());
						profile.put("head_url", SystemConfig.SERVER_DOMAIN+users.getHead_url());
						userjson.put("d", profile);
					}
				}
				array.add(userjson);
			}
			r = 1;
			json.put("d", array);
			break;
		case "friend_search":
			int user_id=data.getInt("user_id");
			keyword = data.getString("key_word");
			list = UsersService.getInstance().searchByKeyword(keyword,user_id);
			array = new JSONArray();
			for (UsersEntity users : list) {
				UsersHXEntity hx = UsersService.getInstance().findHXUserByUserId(users.getId());
				JSONObject userjson = new JSONObject();
				userjson.put("id", users.getId());
				userjson.put("loginname", users.getLoginname());
				userjson.put("is_doc", users.getIs_doc());
				userjson.put("register_time", StrUtil.getYYYYMMDD(users.getRegister_time().getTime()));
				userjson.put("last_login_time", StrUtil.getYYYYMMDD(users.getLast_login_time().getTime()));
				userjson.put("hx_username", hx.getUsername());
				userjson.put("hx_password", hx.getPassword());
				userjson.put("head_url", SystemConfig.SERVER_DOMAIN+users.getHead_url());
				if (users.getIs_doc() == 0) {
					ProfileEntity entity = UsersService.getInstance().getProfileByUserId(users.getId());
					if (entity != null) {
						BindInsulinEntity bindInsulinEntity= InsulinService.getInstance().findInsulinByPatientId(users.getId());
						userjson.put("profile", entity.toJson(users, bindInsulinEntity));
					}
				} else if (users.getIs_doc() == 1) {
					DocProfileEntity docProfileEntity = UsersService.getInstance().findDocProfileByUserId(users.getId());
					if (docProfileEntity != null) {
						JSONObject profile = new JSONObject();
						profile.put("user_id", docProfileEntity.getUser_id());
						profile.put("name", docProfileEntity.getName());
						profile.put("title", docProfileEntity.getTitle());
						profile.put("inst_id", docProfileEntity.getInst_id());
						profile.put("good", docProfileEntity.getGood());
						profile.put("address",docProfileEntity.getAddress());
						profile.put("idcard",docProfileEntity.getIdcard());
						profile.put("nickname",docProfileEntity.getNickname());
						InstitutionEntity keshi = InstService.getInstance().findById(docProfileEntity.getInst_id());
						if (keshi != null) {
							profile.put("keshi", keshi.getName());
							InstitutionEntity bumen = InstService.getInstance().findById(keshi.getPid());
							if (bumen != null) {
								profile.put("bumen", bumen.getName());
								InstitutionEntity yiyuan = InstService.getInstance().findById(bumen.getPid());
								profile.put("yiyuan", yiyuan.getName());
							}
						}
						profile.put("memo", docProfileEntity.getMemo());
						profile.put("gender",docProfileEntity.getGender());
						profile.put("head_url", SystemConfig.SERVER_DOMAIN+users.getHead_url());
						userjson.put("d", profile);
					}
				}
				array.add(userjson);
			}
			r = 1;
			json.put("d", array);
			break;
		}
		
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
